package com.crm.model.dto;

import com.alibaba.fastjson.JSONObject;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.customerManager.CustomerRawData;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.vo.customerManager.UserCustomerImportVO;
import com.crm.service.customerManager.CustomerRawDataDbService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;

/**
 * @ Author ：pym
 * @ Date   ：2021/7/13 13:31
 */
@Slf4j
public class InsertCustomerRawDataCallable implements Callable<JSONObject> {

    private CustomerRawDataDbService customerRawDataDbService;
    private List<UserCustomerImportVO> list;
    private CountDownLatch begin;
    private CountDownLatch end;
    private Integer index;

    public InsertCustomerRawDataCallable(CustomerRawDataDbService customerRawDataDbService, List<UserCustomerImportVO> list,
                                      CountDownLatch begin, CountDownLatch end, Integer index) {
        this.customerRawDataDbService = customerRawDataDbService;
        this.list = list;
        this.begin = begin;
        this.end = end;
        this.index = index;
    }

    @Override
    public JSONObject call() {
        //导入数量
        int importNum = 0;
        //失败行数
        List<Integer> importFailIdStr = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            UserCustomerImportVO userCustomerImportVO = list.get(i);
            int j = (i + 2) + index * 100;
            try {
                CustomerRawData customerRawData = getCustomerRawData(userCustomerImportVO);
                Boolean insertResult = customerRawDataDbService.insertCustomerRawData(customerRawData);
                if (insertResult) {
                    //导入成功
                    importNum = importNum + 1;
                } else {
                    //导入失败
                    importFailIdStr.add(j);
                }
            } catch (Exception e) {
                //导入失败
                importFailIdStr.add(j);
                log.error("InsertCustomerRawDataCallable importCustomer error:", e);
            }
            end.countDown();
        }
        JSONObject result = new JSONObject();
        result.put("importNum",importNum);
        result.put("importFailIdStr",importFailIdStr);
        return result;
    }

    /**
     * 获取客户原始数据
     * @Author pym
     * @Date  2021/7/13 15:08
     * @param userCustomerImportVO 客户原始数据
     * @return {@link {com.crm.model.entity.customerManager.CustomerRawData}}
     */
    private CustomerRawData getCustomerRawData(UserCustomerImportVO userCustomerImportVO) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        String accountId = userAccount.getAccountId();
        CustomerRawData customerRawData = new CustomerRawData();
        customerRawData.setCustomerRawDataId(CommonUtils.getGUID());//主键
        customerRawData.setCompanyName(userCustomerImportVO.getCompanyName());//所属分公司
        customerRawData.setOwner(userCustomerImportVO.getOwner());//所有人
        customerRawData.setCustomerNumber(userCustomerImportVO.getCustomerNumber());//客户编号
        customerRawData.setCustomerName(userCustomerImportVO.getCustomerName());//客户名称
        customerRawData.setUnifiedSocialCreditCode(userCustomerImportVO.getUnifiedSocialCreditCode());//统一社会信用代码
        customerRawData.setDealStatus(userCustomerImportVO.getDealStatus());//成交状态
        customerRawData.setCustomerLevel(userCustomerImportVO.getCustomerLevel());//客户级别
        customerRawData.setCustomerStatus(userCustomerImportVO.getCustomerState());//客户状态
        customerRawData.setCustomerType(userCustomerImportVO.getCustomerType());//客户类型
        customerRawData.setMember(userCustomerImportVO.getMember());//会员
        customerRawData.setPrimaryContactName(userCustomerImportVO.getPrimaryContactName());//主联系人姓名
        customerRawData.setPrimaryContactPhone(userCustomerImportVO.getPrimaryContactPhone());//主联系人电话
        customerRawData.setPrimaryContactPosition(userCustomerImportVO.getPrimaryContactPosition());//主联系人职位
        customerRawData.setPrimaryContactEmail(userCustomerImportVO.getPrimaryContactEmail());//主联系人邮箱
        customerRawData.setEmail(userCustomerImportVO.getEmail());//邮箱
        customerRawData.setEnterpriseExternalPhone(userCustomerImportVO.getEnterpriseExternalPhone());//企业对外手机
        customerRawData.setEnterpriseExternalLandline(userCustomerImportVO.getEnterpriseExternalLandline());//企业对外座机
        customerRawData.setMobilePhone(userCustomerImportVO.getMobilePhone());//手机
        customerRawData.setInternalTrainingOwner(userCustomerImportVO.getInternalTrainingOwner());//内训所有人
        customerRawData.setNumberOfFinanceDept(userCustomerImportVO.getNumberOfFinanceDept());//财务部人数
        customerRawData.setNumberOfMembers(userCustomerImportVO.getNumberOfMembers());//会员数量
        customerRawData.setNumberOfMembersCards(userCustomerImportVO.getNumberOfMembersCards());//会员卡数量
        customerRawData.setNumberOfContacts(userCustomerImportVO.getNumberOfContacts());//联系人数量
        customerRawData.setInvalidMembershipCard(userCustomerImportVO.getInvalidMembershipCard());//失效的会员卡
        customerRawData.setCreateTime(new Date());//创建时间
        customerRawData.setCreatePeopleId(accountId);//创建人id
        customerRawData.setUpdateTime(new Date());//更新时间
        customerRawData.setUpdatePeopleId(accountId);//更新人id
        customerRawData.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));//删除状态---0: 正常; 1: 已删除
        customerRawData.setIdentification(userCustomerImportVO.getIdentification());//删除标识
        return customerRawData;
    }
}
